new Vue({
    el: '#app',
    data() {
        return {
            isflageIOX: true,
            searchWidth: 24,
            historytotal: 0,
            loading: false,
            isSearchFocused: false,
            finished: false,
            historyCurrent: 1,
            searchParams: "",
            clipboard: {
                content: "",
            },
            pptList: [
            ],
            showSearch: false,

            currentItem: null,
            showMore: false, //展开按钮
            options: [{
                    id: 1,
                    name: '发送邮箱',
                    icon: '../../../image/voiceSend.png'
                },
                {
                    id: 2,
                    name: '同步内网网站',
                    icon: '../../../image/voiceTb.png'
                },
                {
                    id: 3,
                    name: '分享到i国网',
                    icon: '../../../image/voiceJ.png'
                },
                {
                    id: 4,
                    name: '重命名',
                    icon: '../../../image/voiceC.png'
                },
                {
                    id: 5,
                    name: '删除',
                    icon: '../../../image/voiceD.png'
                },
                {
                    id: 6,
                    name: '预览',
                    icon: '../../../image/voiceL.png'
                },
            ],
            newTitleName: '',
            showPopName: false
        }
    },
    watch: {

    },
    created() {
        const _this = this
        let jsApiList = []
        $.getScript("../../../js/jweixin-sgccigw-1.0.0.js" /*"js/jweixin-1.0.0.js"*/, function () {
            'use strict';
            wx.ready(function () {
                wx.onHistoryBack(() => {
                    _this.home()
                    return false
                })
            });
            jsApiList = [
                'previewFile',
                'onHistoryBack',
            ];
            wx.config({
                beta: true, // 调用wx.invoke形式的接口值时，该值必须为true。
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: corpid,
                timestamp: '1626832073',
                nonceStr: nonceStr,
                signature: signature,
                jsApiList: jsApiList
            });
        });
    },
    mounted() {
        this.compatibleInput()
        this.queryList()
    },
    methods: {
        preview(item) {
            myPreviewFile(item)
        },
        //显示搜索框或者直接搜索
        searchShow(){
            this.showSearch = true;
            this.isSearchFocused = true;
        },
        queryList() {
            this.finished = false;
            const userId = sessionStorage.getItem("userId");
            let param = {
                "userId": userId,
                "fileName": this.searchParams,
                "current": this.historyCurrent,
                "size": 20,
                "timestamp": new Date().getTime() + "",
            };
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            data = JSON.stringify(param)
            let sign = sm3(data);
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/ppt/queryList",
                type: "post",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                            }
                            this.historytotal = res.resultValue.total
                            res.resultValue.list.forEach((item) => {
                                this.$set(item, 'showSwipeLeft', true)
                                this.pptList.push(item)
                            })
                            if (this.historytotal == 0) {
                                this.finished = true;
                            }
                            if (res.resultValue.list.length < 20) {
                                this.finished = true;
                            }
                            this.loading = false;
                            this.cleanData()
                        } else {
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        }
                    }
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        delConfirm(item) {
            var btnArray = ["取消", "确定"];
            var i = 0;
            mui.confirm("删除文件后不可恢复", "删除文件", btnArray, (e) => {
                if (i > 0) return;
                i++;
                if (e.index == 1) {
                } else {
                    $(".curtain").css("display", "block");
                    const userId = sessionStorage.getItem("userId");
                    let param = {
                        "userId": userId,
                        "id": item.url,
                        "timestamp": new Date().getTime() + "",
                    };
                    let AuthenticationSession = sessionStorage.getItem("Authentication");
                    data = JSON.stringify(param)
                    let sign = sm3(data);
                    console.log("1111")
                    $.ajax({
                        url: localUrlLink + iscp_port + "/tjAiAssistant/ppt/delete",
                        type: "post",
                        async: false, //是否同步，false表示同步请求
                        dataType: "json",
                        data: data,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8",
                            Authentication: AuthenticationSession,
                            sign: sign
                        },
                        success: (res, txt, xhr) => {
                            $(".curtain").css("display", "none");
                            if (res.code == 200) {
                                this.pptList = []
                                this.historyCurrent = 1
                                this.queryList()
                            } else {
                                mui.toast('请稍后再试', {
                                    duration: 1000,
                                    type: 'div'
                                })
                            }
                        },
                        error: (e) => {
                            $(".curtain").css("display", "none");
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        },
                    });
                }
            });
        },
        topptView(type) {
            $(location).attr("href", "./"+type+".html");
        },
        handleFocus() {
            this.isSearchFocused = true;
            this.searchWidth = 20;
            console.log("isSearchFocused",this.isSearchFocused);
        },
        handleBlur() {
            this.isSearchFocused = false;
            this.searchWidth = 24;
            console.log("isSearchFocused",this.isSearchFocused);
        },
        onSearch() {
            this.pptList = [];
            this.historyCurrent = 1;
            this.queryList();
            // this.searchParams = '';
        },
        onCancel() {
            this.searchParams = '';
            this.showSearch = false;
            this.isSearchFocused = false;
            this.searchWidth = 24;
            this.queryList();
        },
        cleanData() {
            this.searchParams = '';
            this.showSearch = false;
            this.isSearchFocused = false;
            this.searchWidth = 24;
            // this.queryList();
        },
        onLoad() {
            if (!this.historyCurrent === 0) {
                this.finished = this.pptList.length >= this.historytotal;

            }
            if (!this.finished) {
                this.historyCurrent = this.historyCurrent + 1;
                this.queryList();
            }
        },
        //分享
        share(result) {
            if(!result.id) {
                return
            }
            shareIgw({
                sceneNo: '109',
                shareType: 1,
                attchId: result.id
            })
        },
        //发送邮件
        sendMail(result) {
            if(!result.id) {
                return
            }
            mySendMail('直接生成PPT',"answerAttach=" + result.id,10,9)
        },
        //发送到内网统一入口
        syncToInner(result) {
            mySyncToInner('直接生成PPT',"answerAttach=" + result.id,10,9)
        },
        //复制按钮
        copy() {
            let text1 = this.clipboard.content;
            var clipboard = new Clipboard(".tag-read", {
                // 通过target指定要复印的节点
                text: function () {
                    return text1;
                },
            });
            clipboard.on("success", (e) => {
                addOperator(2, 11, "复制文本", 1, 1, "复制成功");
                mui.toast("复制成功", {
                    duration: "long",
                    type: "div"
                });
            });
            clipboard.on("error", (e) => {
                addOperator(2, 11, "复制文本", 1, 0, "复制失败");
            });
        },
        handleClickMore(item) {
            this.currentItem = item
            this.showMore = true
        },
        handleRename(item) {
            this.showPopName = true
            let titleName = this.currentItem.name.substr(0, this.currentItem.name.indexOf(".pptx"))
            this.newTitleName = titleName
        },
        reName(item) {
            $(".curtain").css("display", "block");
            const userId = sessionStorage.getItem("userId");
            let param = {
                "userId":userId,
                "id": this.currentItem.id,
                "newName": this.newTitleName,
                "timestamp": new Date().getTime() + "",
            }
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            data = JSON.stringify(param)
            let sign = sm3(data);
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/common/renameFile",
                type: "post",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                            }
                            this.$set(this.currentItem, 'name', this.newTitleName + ".pptx")
                            this.showPopName = false
                            mui.toast('修改成功', {
                                duration: 1000,
                                type: 'div'
                            })
                        } else {
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        }
                    }
                    $(".curtain").css("display", "none");
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        onOptionSelect(option) {
            this.showMore = false
            if(1 == option.id) {
                this.sendMail(this.currentItem)
            }else if(2 == option.id) {
                this.syncToInner(this.currentItem)
            }else if(3 == option.id) {
                this.share(this.currentItem)
            }else if(4 == option.id) {
                this.handleRename(this.currentItem)
            }else if(5 == option.id) {
                this.delConfirm(this.currentItem)
            }else if(6 == option.id) {
                this.preview(this.currentItem)
            }
        },
        // 判断安卓还是ios
        compatibleInput() {
            //获取浏览器的userAgent,并转化为小写
            var ua = navigator.userAgent.toLowerCase();
            //判断是否是苹果手机，是则是true
            var isIos = ua.indexOf("iphone") != -1 || ua.indexOf("ipad") != -1;
            if (isIos) {
                this.isflageIOX = true;
            } else {
                this.isflageIOX = false;
            }
        },
        home() {
            $(location).attr("href", "../../Home.html");
        },
        createSgin(data) {
            let hashData = sm3(data);
            let hashData_last = sm3(hashData);
            hashData_last = hashData_last.substr(0, 5);
            hashData += hashData_last;
            let sign = hashData;
            return sign;
        },
        deleteAllPptFiles(){
            var btnArray = ["取消", "确定"];
            var i = 0;
            mui.confirm("清空我的文档", "清空文件", btnArray, (e) => {
                if (i > 0) return;
                i++;
                if (e.index == 1) {
                    $(".curtain").css("display", "block");
                    const userId = sessionStorage.getItem("userId");
                    let param = {
                        "userId": userId,
                        "timestamp": new Date().getTime() + "",
                    };
                    let AuthenticationSession = sessionStorage.getItem("Authentication");
                    data = JSON.stringify(param)
                    let sign = sm3(data);
                    $.ajax({
                        url: localUrlLink + iscp_port + "/tjAiAssistant/ppt/deleteAllPptFiles",
                        type: "post",
                        async: false, //是否同步，false表示同步请求
                        dataType: "json",
                        data: data,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8",
                            Authentication: AuthenticationSession,
                            sign: sign
                        },
                        success: (res, txt, xhr) => {
                            $(".curtain").css("display", "none");
                            if (res.code == 200) {
                                this.pptList = []
                                this.historyCurrent = 1
                                this.queryList()
                            } else {
                                mui.toast('请稍后再试', {
                                    duration: 1000,
                                    type: 'div'
                                })
                            }
                        },
                        error: (e) => {
                            $(".curtain").css("display", "none");
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        },
                    });
                } else {
                }
            });
        },
        openSwipe(item) {
            this.$set(item, 'showSwipeLeft', false)
        },
        closeSwipe(item) {
            this.$set(item, 'showSwipeLeft', true)
        },
    }
})